package com.leo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: HandlerInterceptor3
 * @Description: 实现HandlerInterceptor的拦截器实现
 * @Author: leo825
 * @Date: 2020-02-08 20:10
 * @Version: 1.0
 */
public class HandlerInterceptor3 implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("HandlerInterceptor3 preHandle");
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("startTime",System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("HandlerInterceptor3 postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("HandlerInterceptor3 afterCompletion");
        HttpSession session = httpServletRequest.getSession();
        long startTime = (long)session.getAttribute("startTime");
        System.out.println("HandlerInterceptor3 过滤的接口耗时：" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
